#!/usr/bin/python
from __future__ import print_function    
import subprocess as sp
import os
import time

def main():
    # get the name of the computer
    host = os.uname()[1]
    # directories with the executables
    directories = ['./noTablesNoGPU/','./origNoOpt/', './GPUPlank/', './original/',  './noTablesGPU/']
    # values of w
    sizes = ['8', '16', '32']
    sizeMap = {'8':'20000000', '16':'40000000', '32':'100000000'}
    # empty dictionary for file objects
    output = {}
    # open files  
    for size in sizes:
        output[size] = open(host + '_' + size + '.dat', 'w')
    # run the executables and get the data
    for i in xrange(25):
        print("Working on iteration %d" %(i))
        for size in sizes:
            for directory in directories:
                print("size = %s, code = %s" % (size, directory))
                btime = time.time()
                if directory in ['./origNoOpt/','./orignial/', './noTablesNoGPU/']:
                    cmd_str = [directory + 'gf_basic_tester', size, 'default', '1']
                else:
                    cmd_str = [directory + 'gf_basic_tester', size, 'default', '1', sizeMap[size]]
                cmd = sp.Popen(cmd_str, stdout=sp.PIPE)
                curr_out = cmd.communicate()[0]
                lines = curr_out.split('\n')
                for line in lines:
                    if line != '':
                        tokens = line.split()
                        output[size].write('%s\t' % tokens[2])
                output[size].flush()
                etime = time.time()
                print ("Time = %f" % (etime - btime))
                print ("Sleeping", end=''),
                time.sleep(5)
                print(' .', end='')
                time.sleep(5)
                print(' .', end='')
                time.sleep(5)
                print(' .')
                time.sleep(5)
            output[size].write('\n')
            output[size].flush()
    for size in sizes:
        output[size].close()

if __name__=='__main__':
    main()
